using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using ZR.Admin.WebApi.Filters;
using ZR.Model;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.ServiceCore.Services.IService;

namespace ZR.Admin.WebApi.Controllers.System
{
    /// <summary>
    /// 报警列表管理
    /// </summary>
    [Verify]
    [Route("system/alarmList")]
    [ApiExplorerSettings(GroupName = "sys")]
    public class AlarmListController : BaseController
    {
        private readonly IAlarmListService _alarmListService;

        public AlarmListController(IAlarmListService alarmListService)
        {
            _alarmListService = alarmListService;
        }

        /// <summary>
        /// 报警列表
        /// </summary>
        [HttpPost("list")]
        public IActionResult List([FromBody] AlarmListQueryDto query)
        {
            var pager = new PagerInfo
            {
                PageNum = query.PageNum,
                PageSize = query.PageSize
            };
            var result = _alarmListService.GetAlarmList(query, pager);
            return SUCCESS(result);
        }

        /// <summary>
        /// 新增报警
        /// </summary>
        [HttpPost("add")]
        [Log(Title = "报警列表", BusinessType = BusinessType.INSERT)]
        public IActionResult Add([FromBody] AlarmListDto dto)
        {
            if (string.IsNullOrEmpty(dto?.Type))
            {
                return ToResponse(ResultCode.FAIL, "类型不能为空");
            }

            var entity = new AlarmList
            {
                Type = dto.Type,
                City = dto.City
            };

            var id = _alarmListService.AddAlarm(entity);
            return SUCCESS(new { Id = id });
        }

        /// <summary>
        /// 删除报警
        /// </summary>
        [HttpDelete("{ids}")]
        [Log(Title = "报警列表", BusinessType = BusinessType.DELETE)]
        public IActionResult Delete(string ids)
        {
            var idList = Tools.SplitAndConvert<int>(ids);
            if (idList == null || idList.Length == 0)
            {
                return ToResponse(ResultCode.FAIL, "请选择要删除的数据");
            }

            var rows = _alarmListService.DeleteAlarms(idList);
            return ToResponse(rows);
        }
    }
}

